import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import {toast} from 'nc-lightapp-front';

export default function getAgreeTableData(isRefresh) {
    const {orgVal, agreeSearchModalValue, agreementPageInfo, pk_psndocList, isQueryTemplate} = this.state;
    const {table} = this.props;
    if (!orgVal.refpk) {
        table.setAllTableData('agreementList', {
            rows: []
        });
        this.updateButtonStatus();
        return;
    }

    return proFetch({
        url: '/nccloud/hrcm/account/QueryAgreementAction.do',
        body: {
            ...agreeSearchModalValue,
            pk_org: orgVal.refpk,
            isQueryTemplate,
            areaCode: 'agreementList',
            pageInfo: agreementPageInfo,
            pk_psndocList,
            isContract: false
        },
    }).then((res) => {
        if (res.success) {
            if (isRefresh) {
                toast({color: "success", content: this.state.json['cm6011-000099']});
            }
            if (res.data && res.data.data && res.data.data.agreementList) {
                table.setAllTableData('agreementList', {
                    rows: res.data.data.agreementList.rows
                });
                if (res.data.data.agreementList.rows.length) {
                    this.props.table.setClickRowIndex('agreementList', {
                        record: res.data.data.agreementList.rows[0].values,
                        index: 0
                    });
                    this.props.table.focusRowByIndex('agreementList', 0);
                }
                this.setState({
                    searched: true,
                    agreementPageInfo: res.data.data.agreementList.pageInfo || {
                        pageSize: 10,
                        pageIndex: 1,
                        total: 0,
                        totalPage: 1
                    }
                }, () => {
                    this.updateButtonStatus();
                });
            } else {
                table.setAllTableData('agreementList', {rows: []});
                this.setState({
                    searched: true,
                    agreementPageInfo: {
                        pageSize: 10,
                        pageIndex: 1,
                        total: 0,
                        totalPage: 1
                    }
                }, () => {
                    this.updateButtonStatus();
                })
            }
        }
    });
}